home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / pipe / pipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-05  |  5.3 KB  |  183 lines

  1. #include "sprite.h"
  2. #include "option.h"
  3. #include "fs.h"
  4. #include "time.h"
  5. #include "sysStats.h"
  6. #include "kernel/sched.h"
  7. #include "io.h"
  8.  
  9. #ifdef UNIX_SYTLE
  10. #include <sys/file.h>
  11. extern    int errno;
  12. #endif
  13.  
  14. int    numIterations = 1000;
  15. int    size = 1;
  16. Boolean devNull = FALSE;
  17. Boolean copyTest = FALSE;
  18. Boolean getTime = FALSE;
  19. Boolean getPid = FALSE;
  20. Boolean nonBlockingTest = FALSE;
  21.  
  22. Option optionArray[] = {
  23.     {OPT_INT, 'n', (Address)&numIterations, 
  24.      "Number of iterations (Default 1000)."},
  25.     {OPT_INT, 'd', (Address)&size,
  26.      "Size of each read/write."},
  27.     {OPT_TRUE, 'Z', (Address)&devNull,
  28.      "Use /dev/null instead of pipes"},
  29.     {OPT_TRUE, 'C', (Address)©Test,
  30.      "Measure cost of Byte_Copy"},
  31.     {OPT_TRUE, 'T', (Address)&getTime,
  32.      "Measure cost of Sys_GetTimeOfDay"},
  33.     {OPT_TRUE, 'P', (Address)&getPid,
  34.      "Measure cost of Proc_GetIDs"},
  35.     {OPT_TRUE, 'B', (Address)&nonBlockingTest,
  36.      "Test non-blocking writes"},
  37. };
  38. int    numOptions = Opt_Number(optionArray);
  39. int    i = 0;
  40. Sched_Instrument startSchedStats, endSchedStats;
  41. char buffer[4096];
  42.  
  43. main(argc, argv)
  44.     int     argc;
  45.     char *argv[];
  46. {
  47.     register    int    numTimes;
  48.     Time        before, after;
  49.     int            parIn, parOut, childIn, childOut;
  50.     int            count;
  51.     int            pid;
  52.  
  53.     (void)Opt_Parse(&argc, argv, numOptions, optionArray);
  54.     numTimes = numIterations;
  55.     if (devNull) {
  56.     /* 
  57.      * Measure the cost of N read & write calls on "/dev/null"
  58.      */
  59.     Fs_Open("/dev/null", FS_READ, 0, &childOut);
  60.     Fs_Open("/dev/null", FS_WRITE, 0, &childIn);
  61.     Sys_Stats(SYS_SCHED_STATS, 0, &startSchedStats);
  62.     Sys_GetTimeOfDay(&before, NULL, NULL);
  63.     while (numTimes > 0) {
  64.         Fs_Read(childIn, size, buffer, &count);
  65.         Fs_Write(childOut, size, buffer, &count);
  66.         numTimes--;
  67.     }
  68.     Sys_GetTimeOfDay(&after, NULL, NULL);
  69.     Sys_Stats(SYS_SCHED_STATS, 0, &endSchedStats);
  70.     Time_Subtract(after, before, &after);
  71.     printf("%d read-writes of /dev/null at %dus each\n", numIterations,
  72.         (after.seconds * 1000000 + after.microseconds) / numIterations);
  73.     PrintIdleTime(io_StdErr, &startSchedStats, &endSchedStats, &after);
  74.     } else if (copyTest) {
  75.     /* 
  76.      * Measure the cost of N read & write calls on "/dev/null"
  77.      */
  78.     char dest[5000];
  79.     Sys_Stats(SYS_SCHED_STATS, 0, &startSchedStats);
  80.     Sys_GetTimeOfDay(&before, NULL, NULL);
  81.     while (numTimes > 0) {
  82.         Byte_Copy(size, buffer, dest);
  83.         numTimes--;
  84.     }
  85.     Sys_GetTimeOfDay(&after, NULL, NULL);
  86.     Sys_Stats(SYS_SCHED_STATS, 0, &endSchedStats);
  87.     Time_Subtract(after, before, &after);
  88.     printf("%d Byte_Copy of %d bytes at %dus each\n", numIterations, size,
  89.         (after.seconds * 1000000 + after.microseconds) / numIterations);
  90.     PrintIdleTime(io_StdErr, &startSchedStats, &endSchedStats, &after);
  91.     } else if (getTime) {
  92.     /*
  93.      * Measure the cost of N calls to Sys_GetTimeOfDay
  94.      */
  95.     Sys_Stats(SYS_SCHED_STATS, 0, &startSchedStats);
  96.     Sys_GetTimeOfDay(&before, NULL, NULL);
  97.     while (numTimes > 0) {
  98.         Sys_GetTimeOfDay(&after, NULL, NULL);
  99.         numTimes--;
  100.     }
  101.     Sys_GetTimeOfDay(&after, NULL, NULL);
  102.     Sys_Stats(SYS_SCHED_STATS, 0, &endSchedStats);
  103.     Time_Subtract(after, before, &after);
  104.     printf("%d Sys_GetTimeOfDay at %dus each\n", numIterations,
  105.         (after.seconds * 1000000 + after.microseconds) / numIterations);
  106.     PrintIdleTime(io_StdErr, &startSchedStats, &endSchedStats, &after);
  107.     } else if (getPid) {
  108.     /*
  109.      * Measure the cost of N calls to Proc_GetIDs
  110.      */
  111.     int pid;
  112.  
  113.     Sys_Stats(SYS_SCHED_STATS, 0, &startSchedStats);
  114.     Sys_GetTimeOfDay(&before, NULL, NULL);
  115.     while (numTimes > 0) {
  116.         Proc_GetIDs(&pid, NULL, NULL, NULL);
  117.         numTimes--;
  118.     }
  119.     Sys_GetTimeOfDay(&after, NULL, NULL);
  120.     Sys_Stats(SYS_SCHED_STATS, 0, &endSchedStats);
  121.     Time_Subtract(after, before, &after);
  122.     printf("%d Proc_GetIDs at %dus each\n", numIterations,
  123.         (after.seconds * 1000000 + after.microseconds) / numIterations);
  124.     PrintIdleTime(io_StdErr, &startSchedStats, &endSchedStats, &after);
  125.     } else if (nonBlockingTest) {
  126.     /*
  127.      * Verify the behavior of non-blocking writes.
  128.      */
  129.     char buffer[5000];
  130. #ifdef UNIX_STYLE 
  131.     int id[2];
  132.     int count;
  133.  
  134.     pipe(id);
  135.     fcntl(id[1], F_SETFL, FNDELAY);
  136.     count = write(id[1], buffer, 5000);
  137.     if (count < 0) {
  138.         printf("Error %d\n", errno);
  139.     } else {
  140.         printf("Transferred %d bytes\n", count);
  141.     }
  142. #else
  143.     int readID, writeID, count;
  144.     ReturnStatus status;
  145.  
  146.     Fs_CreatePipe(&readID, &writeID);
  147.     Ioc_SetBits(writeID, IOC_NON_BLOCKING);
  148.     status = Fs_Write(writeID, sizeof(buffer), buffer, &count);
  149.     Io_Print("Transferred %d bytes\n", count);
  150.     if (status != SUCCESS) {
  151.         Stat_PrintMsg(status, "Fs_Write: ");
  152.     }
  153. #endif
  154.     } else {
  155.     /*
  156.      * Measure the cost of N exchanges of D bytes via pipes.
  157.      */
  158.     Fs_CreatePipe(&parIn, &childOut);
  159.     Fs_CreatePipe(&childIn, &parOut);
  160.     if (Proc_Fork(TRUE, &pid) == PROC_CHILD_PROC) {
  161.         while (numTimes > 0) {
  162.         Fs_Read(childIn, size, buffer, &count);
  163.         Fs_Write(childOut, size, buffer, &count);
  164.         numTimes--;
  165.         }
  166.     } else {
  167.         Sys_Stats(SYS_SCHED_STATS, 0, &startSchedStats);
  168.         Sys_GetTimeOfDay(&before, NULL, NULL);
  169.         while (numTimes > 0) {
  170.         Fs_Write(parOut, size, buffer, &count);
  171.         Fs_Read(parIn, size, buffer, &count);
  172.         numTimes--;
  173.         }
  174.         Sys_GetTimeOfDay(&after, NULL, NULL);
  175.         Sys_Stats(SYS_SCHED_STATS, 0, &endSchedStats);
  176.         Time_Subtract(after, before, &after);
  177.         printf("%d read-writes at %dus each\n", numIterations,
  178.         (after.seconds * 1000000 + after.microseconds) / numIterations);
  179.         PrintIdleTime(io_StdErr, &startSchedStats, &endSchedStats, &after);
  180.     }
  181.     }
  182. }
  183.